<template>
  <!-- 活动评分 -->
  <div>
    <!-- 表单 -->
    <custom-form :formList="formList" :form="form" editModel="V"> </custom-form>
    <!-- 评分表 -->
    <a-table :columns="columns" :data-source="list" bordered :pagination="false">
      <template slot="score" slot-scope="text, record, index">
        <span v-if="editModel === 'V'">{{ text }}</span>
        <BaseInputNumber
          v-else
          v-model="record.score"
          :placeholder="`请输入${index === list.length - 1 ? '扣' : '得'}分`"
          :precision="0"
          :min="index === list.length - 1 ? 0 : 1"
          :max="index === list.length - 1 ? 8 : record.totalScore"
        />
      </template>
    </a-table>
  </div>
</template>

<script>
import { dataList } from './evalueList' // 基础配置
export default {
  name:'a'+ Date.now(),
  // name: 'ActivityEvalue',
  data() {
    return {
      dataList: this.$clone(dataList[this.tableType]),
      form: {
        creator: this.$userInfo().name,
        createTime: this.$dateFormat(new Date(), 'D'),
        totalScore: this.total,
      }, // 表单
      formList: [
        {
          label: '考评人',
          keyName: 'creator',
        },
        {
          label: '日期',
          keyName: 'createTime',
        },
        {
          label: '总分',
          keyName: 'totalScore',
        },
      ], // 表单列表
    }
  },
  props: {
    // 操作类型，当为V时只能查看
    editModel: {
      type: String,
      default: () => 'V',
    },
    // 表格类型
    tableType: {
      type: String,
      default: () => '',
    },
    // 得分列表
    scoreList: {
      type: Array,
      default: () => [],
    },
    // 总分
    total: {
      type: [String, Number],
      default: () => '0',
    },
  },
  watch: {
    list: {
      deep: true,
      handler(e) {
        if (!this.total || this.total == 0) {
          let totalScore = e
            .map((item) => item.score)
            .reduce((total, num, index) => {
              return index == e.length - 1 ? Number(total) - (num || 0) : Number(total) + (num || 0)
            })
          this.form.totalScore = totalScore < 0 ? '0' : totalScore
        }
        this.$emit(
          'update:scoreList',
          e.map((item) => item.score)
        )
      },
    },
  },
  mounted() {
    if (this.scoreList?.length > 0) {
      for (let index in this.scoreList) {
        this.$set(this.dataList[index], 'score', this.scoreList[index])
      }
    }
  },
  computed: {
    // 列表
    list() {
      return this.dataList
    },
    // 表头
    columns() {
      let columns = [
        {
          title: `廉洁文化${['进项目', '进岗位', '进班子', '进家庭'][this.tableType] || ''}示范点建设考核评分表`,
          dataIndex: 'time',
          children: [
            {
              title: '考评项目',
              dataIndex: 'name',
              width: 100,
              colSpan: 2,
              customRender: (value, row, index) => {
                const obj = {
                  children: value,
                  attrs: {},
                }
                obj.attrs.colSpan = 2
                if (this.tableType === '0') {
                  if (index === 2) {
                    obj.attrs.rowSpan = 11
                    obj.attrs.colSpan = 1
                  } else if (index === 13) {
                    obj.attrs.rowSpan = 5
                  } else if ((index > 2 && index < 13) || (index > 13 && index < 18)) {
                    obj.attrs.rowSpan = 0
                  }
                } else if (this.tableType === '1') {
                  if (index === 2) {
                    obj.attrs.rowSpan = 9
                    obj.attrs.colSpan = 1
                  } else if (index === 11) {
                    obj.attrs.rowSpan = 5
                  } else if ((index > 2 && index < 11) || (index > 11 && index < 16)) {
                    obj.attrs.rowSpan = 0
                  }
                } else if (this.tableType === '2') {
                  if (index === 2) {
                    obj.attrs.rowSpan = 13
                    obj.attrs.colSpan = 1
                  } else if (index === 15) {
                    obj.attrs.rowSpan = 5
                  } else if ((index > 2 && index < 15) || (index > 15 && index < 20)) {
                    obj.attrs.rowSpan = 0
                  }
                } else if (this.tableType === '3') {
                  if (index === 2) {
                    obj.attrs.rowSpan = 7
                    obj.attrs.colSpan = 1
                  } else if (index === 9) {
                    obj.attrs.rowSpan = 5
                  } else if ((index > 2 && index < 9) || (index > 9 && index < 14)) {
                    obj.attrs.rowSpan = 0
                  }
                }

                return obj
              },
            },
            {
              colSpan: 0,
              width: 150,
              dataIndex: 'subTitle',
              customRender: (value, row, index) => {
                const obj = {
                  children: value,
                  attrs: {},
                }
                if (this.tableType === '0') {
                  if (index < 2 || index > 12) {
                    obj.attrs.colSpan = 0
                  } else if (index === 2) {
                    obj.attrs.rowSpan = 7
                  } else if (index === 9) {
                    obj.attrs.rowSpan = 2
                  } else if ((index > 2 && index < 9) || (index > 9 && index < 11)) {
                    obj.attrs.rowSpan = 0
                  }
                } else if (this.tableType === '1') {
                  if (index < 2 || index > 12) {
                    obj.attrs.colSpan = 0
                  } else if (index === 2) {
                    obj.attrs.rowSpan = 6
                  } else if ((index > 2 && index < 8) || (index > 10 && index < 14)) {
                    obj.attrs.rowSpan = 0
                  }
                } else if (this.tableType === '2') {
                  if (index < 2 || index > 14) {
                    obj.attrs.colSpan = 0
                  } else if (index === 2) {
                    obj.attrs.rowSpan = 5
                  } else if (index === 7) {
                    obj.attrs.rowSpan = 5
                  } else if (index === 12) {
                    obj.attrs.rowSpan = 3
                  } else if (index === 12) {
                    obj.attrs.rowSpan = 3
                  } else if ((index > 2 && index < 7) || (index > 7 && index < 12) || (index > 12 && index < 15)) {
                    obj.attrs.rowSpan = 0
                  }
                } else if (this.tableType === '3') {
                  if (index < 2 || index > 8) {
                    obj.attrs.colSpan = 0
                  } else if (index === 2) {
                    obj.attrs.rowSpan = 5
                  } else if (index === 7) {
                    obj.attrs.rowSpan = 2
                  } else if ((index > 2 && index < 7) || (index > 7 && index < 11)) {
                    obj.attrs.rowSpan = 0
                  }
                }
                return obj
              },
            },
            {
              title: '考评细则',
              width: 600,
              dataIndex: 'desc',
            },
            {
              title: '评分标准',
              dataIndex: 'rule',
            },
            {
              title: '分值',
              width: 80,
              dataIndex: 'totalScore',
              customRender() {
                return arguments[0] ? arguments[0] + '分' : ''
              },
            },
            {
              title: '得分',
              width: 300,
              align: 'center',
              scopedSlots: { customRender: 'score' },
              dataIndex: 'score',
            },
          ],
        },
      ]
      return columns
    },
  },
}
</script>
